package com.aoasaa;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.aoasaa.CreatingTextNotes.MyAsyncTask123;
import com.userrequestdtos.ViewingCategoryAndTitleDTO;

import android.content.Context;

public class UserXMLParsingFile {
	static Context conn;
	public static String[] responseOperation(Context con,String response,String v_tagname, String v_elementtagname)
	{          
		conn=con;
		String arr_response[]=new String[1];
		String arr_response2[]=new String[1];
		try   
		{ 
			Document doc = null;
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(response));
			doc = db.parse(is); 

			NodeList labTestList = doc.getElementsByTagName("verifycheck");
			Element verify=(Element) labTestList.item(0);
			String conditionText = verify.getFirstChild().getNodeValue();
			// if any server problem is there then
			if(conditionText.equals("serverproblem"))
			{
				UserAlertDisplay.displayAlert(conn, "ERROR", conditionText);
			} 

			// while creating a new category responses may be as follows
			else if(conditionText.equals("category already exists"))
			{
				UserAlertDisplay.displayAlert(conn, "ERROR", conditionText);
				arr_response[0]=conditionText;
			}
			else if(conditionText.equals("created category"))
			{
				UserAlertDisplay.displayAlert(conn, "Sucess", conditionText);
				arr_response[0]=conditionText;
			}
			else if(conditionText.equals("unable to create category"))
			{
				UserAlertDisplay.displayAlert(conn, "ERROR", conditionText);
				arr_response[0]=conditionText;
			}

			// while saving text notes, image notes and audio notes responses may be as follows

			else if(conditionText.equals("title_exists"))
			{
				UserAlertDisplay.displayAlert(conn, "ERROR", "Title already Exists");
				arr_response[0]=conditionText;
			}
			else if(conditionText.equals("inserted sucessfully"))
			{
				UserAlertDisplay.displayAlert(conn, "Sucess", " Note saved Sucessfully ");
				arr_response[0]=conditionText;
			}
			else if(conditionText.equals("unable to insert"))
			{
				UserAlertDisplay.displayAlert(conn, "ERROR", "Unable to save the note");
				arr_response[0]=conditionText;
			}  
			// while fetching view category list 
			else if(conditionText.equals("datapresent"))
			{      
			//	System.out.println(conditionText);
				NodeList userdataList = doc.getElementsByTagName("userdata");
				for(int i=0;i<userdataList.getLength();i++)
				{    
					Element userElement=(Element) userdataList.item(i);
					//String userAttribute=userElement.getAttribute("categoryname");
					//System.out.println(userAttribute);
					NodeList valueList = userElement.getElementsByTagName(v_elementtagname);
					arr_response=new String[valueList.getLength()];
					for (int j = 0; j < valueList.getLength(); ++j)
					{              
						Element value = (Element) valueList.item(j);
						String conditionText1 = value.getFirstChild().getNodeValue();
						System.out.println(conditionText1);
						arr_response[j]=conditionText1;   
					} 
					NodeList valueList2 = userElement.getElementsByTagName(v_tagname);
					for (int j = 0; j < valueList2.getLength(); ++j)
					{   
						arr_response2=new String[2];
						Element value = (Element) valueList2.item(j);
						String conditionText1 = value.getFirstChild().getNodeValue();
						System.out.println(conditionText1); 
						arr_response2[0]=arr_response[0]; 
						arr_response2[1]=conditionText1;
						//arr_response=arr_response2;
						return arr_response2;    
					}
					System.out.println(arr_response); 
				} 
 
			}
			else if(conditionText.equals("nodata"))
			{ 
				// alert
			} 
			
			// 

		}     
		catch (Exception e) 
		{
			e.printStackTrace();   
		}
		return arr_response;
	}
}
